﻿using System;
using Hjt212Parser;
using log4net;
using TcpServer.Facade;
using TcpServer.Orm;

namespace TcpServer.DataProcessor
{
    public class PersistenceProcessor:IProcessor
    {
        private readonly ILog log = LogManager.GetLogger(typeof(PersistenceProcessor));

        public void Persist()
        {
            throw new NotImplementedException();
        }

        public void Persist(DustEntities context, SocketClientInfo content)
        {
            throw new NotImplementedException();
        }

        public void Persist(DataCommunicationEntities context, SocketClientInfo content)
        {
            try
            {
                var dateTime = Parser.GetDateTimeByKey("DataTime", content.ReceivedMsg);
                if(dateTime==null) return;
                var receivedData = new ReceivedString()
                {
                    ReceivedDateTime = DateTime.Now,
                    ReceivedOriginString = content.ReceivedMsg,
                    DataTime = dateTime.Value,
                    length = Parser.GetIntByKey("Length", content.ReceivedMsg),
                    ST = Parser.GetStringByKey("ST", content.ReceivedMsg),
                    QN = Parser.GetStringByKey("QN", content.ReceivedMsg),
                    CN = Parser.GetStringByKey("CN", content.ReceivedMsg),
                    MN = Parser.GetStringByKey("MN", content.ReceivedMsg),
                    Flag = Parser.GetStringByKey("Flag", content.ReceivedMsg),
                    CP = Parser.GetStringByKey("CP", content.ReceivedMsg),
                    CRC = Parser.GetStringByKey("Crc", content.ReceivedMsg),
                    IsExcute = "F",
                    IsProcessed = false,
                    IsLengthRight = Parser.IsLengthRight(content.ReceivedMsg),
                    IsCrcRight = Parser.IsCrcRight(content.ReceivedMsg),
                    qnRtn = "F",
                    exeRtn = "F"
                };
                context.ReceivedStrings.Add(receivedData);
            }
            catch (Exception ex)
            {
                log.Error( ex);
            }
        }
    }
}

